// 生产token的jwt
const jwt = require('jsonwebtoken')

// 引入 goods 表
const goods = require('../../models/goods')

// 查询分类
getCategory = (req,res)=>{
  // console.log(req.query)
  const temp = req.headers.authorization
  const temp1 = temp.split(' ')
  const token = temp1[1]
  jwt.verify(token,'quan_and_huan',(err, authData) => {
    if(err) {
      res.json({code:598,msg:'token失效,请重新登录!'});
    }else{
      // console.log(req.params)
      // token验证成功之后
      goods.find({
        goodsType:req.params.id ? req.params.id : '',
      }).then(result=>{
        res.json({code:200,data:result})
      })
    }
  });
}
// 查询地区
getCategoryByAddress = (req,res)=>{
  // console.log(req.query)
  const temp = req.headers.authorization
  const temp1 = temp.split(' ')
  const token = temp1[1]
  jwt.verify(token,'quan_and_huan',(err, authData) => {
    if(err) {
      res.json({code:598,msg:'token失效,请重新登录!'});
    }else{
      // console.log(req.params)
      // token验证成功之后
      goods.find({
        goodsAddress:{$regex: req.params.id ? req.params.id : '',$options:'i'},
      }).then(result=>{
        res.json({code:200,data:result})
      })
    }
  });
}
// 查询名字
getCategoryByName = (req,res)=>{
  // console.log(req.query)
  const temp = req.headers.authorization
  const temp1 = temp.split(' ')
  const token = temp1[1]
  jwt.verify(token,'quan_and_huan',(err, authData) => {
    if(err) {
      res.json({code:598,msg:'token失效,请重新登录!'});
    }else{
      // console.log(req.params)
      // token验证成功之后
      goods.find({
        goodsname:{$regex: req.params.id ? req.params.id : '',$options:'i'},
      }).then(result=>{
        res.json({code:200,data:result})
      })
    }
  });
}
// 查询id
getDetailById = (req,res)=>{
  // console.log(req.query)
  const temp = req.headers.authorization
  const temp1 = temp.split(' ')
  const token = temp1[1]
  jwt.verify(token,'quan_and_huan',(err, authData) => {
    if(err) {
      res.json({code:598,msg:'token失效,请重新登录!'});
    }else{
      // console.log(req.params)
      // token验证成功之后
      goods.find({
        _id:req.params.id
      }).then(result=>{
        res.json({code:200,data:result})
      })
    }
  });
}
module.exports = {
  getCategory,getCategoryByAddress,getCategoryByName,getDetailById
}